﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public partial class Bill_PayoutRecord : System.Web.UI.Page
{
    //private Order _CurrentOrder;
    ///// <summary>
    ///// 当前页面所拥有的订单实体
    ///// </summary>
    //protected Order CurrentOrder
    //{
    //    get
    //    {
    //        if (_CurrentOrder == null)
    //        {
    //            if (Request.QueryString["OrderId"] == null || Request.QueryString["OrderId"].Length == 0)
    //            {
    //                Response.Redirect("~/Bill", true);
    //            }
    //            int orderId = Convert.ToInt32(Request.QueryString["OrderId"]);
    //            _CurrentOrder = Order.Load(orderId);
    //            if (_CurrentOrder == null)
    //            {
    //                Response.Redirect("~/Bill", true);
    //            }
    //        }
    //        return _CurrentOrder;
    //    }
    //}
    //private MediaInfo _CurrentMediaInfo;
    ///// <summary>
    ///// 当前实时媒体信息
    ///// </summary>
    //protected MediaInfo CurrentMediaInfo
    //{
    //    get 
    //    {
    //        //CurrentOrder.MediaInfo = null;
    //        return CurrentOrder.MediaInfo;
    //    }
    //}

    //#region 当传过来的是OrderId时获取相应信息并绑定
    ///// <summary>
    ///// 当传过来的是OrderId时获取相应信息并绑定
    ///// </summary>
    //public void GetAndBindByOrderId()
    //{
    //    Order odr = Order.Load(Convert.ToInt16(Request.QueryString["OrderId"]));
    //    if (odr.MediaFinanceAudit == Audit.Success || odr.Bill.FinanceAudit == Audit.Success)
    //    {
    //        this.PayoutRecordEditPlaceHolder.Visible = false;
    //        this.gvPayoutListByOrderId.Columns[6].Visible = false;
    //        this.gvPayoutListByOrderId.Columns[7].Visible = false;
    //    }
    //    this.imgBillAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", odr.Bill.FinanceAudit.ToString());
    //    this.imgMediaAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", odr.MediaFinanceAudit.ToString());
    //    this.lblAdvanceBalance.Text = odr.MediaInfo.Advance.ToString("");
    //    this.lblBrokerageBalance.Text = odr.MediaInfo.Brokerage.ToString("");
    //    this.lblMediaName.Text = odr.MediaInfo.Name;
    //    this.lblMediaName.ToolTip = odr.MediaInfoId.ToString() ;
    //    this.lblCustomerName.Text = odr.Bill.Customer.Name;
    //    decimal FinalPayout = 0;
    //    decimal AlreadyPayout = 0;
    //    if (odr.MediaOrders != null)
    //    {
    //        foreach (MediaOrder mo in odr.MediaOrders)
    //        {
    //            FinalPayout += mo.RealPayout;
    //        }
    //    }
    //    if (FinalPayout != 0)
    //    {
    //        lblFinalPayout.Text = FinalPayout.ToString("c");
    //    }
    //    else { lblFinalPayout.Text = "没记录"; }
    //    if (odr.PayoutRecords != null)
    //    {
    //        foreach (PayoutRecord pr in odr.PayoutRecords)
    //        {
    //            AlreadyPayout += pr.Payout + pr.DeductFromAdvance + pr.DeductFromBrokerage;
    //        }
    //    }
    //    if (AlreadyPayout != 0)
    //    {
    //        this.lbAlreadyPayout.Text = AlreadyPayout.ToString("c");
    //    }
    //    else { this.lbAlreadyPayout.Text = "没付款"; }

    //    this.valcAdvanceBalace.ValueToCompare = odr.MediaInfo.Advance.ToString();
    //    this.valcBrokerage.ValueToCompare = odr.MediaInfo.Brokerage.ToString();
    //    if (odr.MediaInfo.Advance <= 0)
    //    {
    //        txtDeductFromAdvance.Text = "没余额可扣";
    //        txtDeductFromAdvance.Enabled = false;
    //        this.valcAdvanceBalace.Enabled = false;
    //    }
    //    if (odr.MediaInfo.Brokerage <= 0)
    //    {
    //        txtDeductFromBrokerage.Text = "没余额可扣";
    //        txtDeductFromBrokerage.Enabled = false;
    //        this.valcBrokerage.Enabled = false;
    //    }
    //}
    //#endregion

    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    if (!IsPostBack)
    //    {
    //        if (Request.UrlReferrer == null)
    //        {
    //            this.Response.Write("请不要非法进入本页！<br/>3秒后将转回....");
    //            this.Response.Write("<script type=\"text/javascript\">setTimeout(\"location.href='../Finance/'\",3*1000);</script>");
    //            this.Response.End();
    //            return;
    //        }
    //        else
    //        {
    //            if (Request.QueryString["OrderId"] == null && Request.QueryString["PayoutRecordId"] == null)
    //            {
    //                Response.Redirect("../Finance/");
    //            }
    //            else
    //            {
    //                this.gvPayoutListByOrderId.DataKeyNames = new string[] { "Id" };
    //                if (Request.QueryString["OrderId"] != null && Request.QueryString["PayoutRecordId"] == null)  //添加新付款的时候
    //                {
    //                    this.txtPayoutDate.Text = DateTime.Now.ToShortDateString();
    //                    this.btnNew.Visible = false;
    //                    this.btnSavePayout.Text = "确认付款";
    //                    this.h1Title.InnerText = "增加新付款";
    //                    this.lblOrderId.Text = Request.QueryString["OrderId"];
    //                    gvPayoutListByOrderId.DataSource = CurrentOrder.PayoutRecords;
    //                    gvPayoutListByOrderId.DataBind();
    //                    GetAndBindByOrderId();

    //                }
    //                else  //当修改付款记录的时候，即传过来的是PayoutRecordId
    //                {
    //                    this.btnNew.Visible = true;
    //                    this.btnSavePayout.Text = "确认修改";
    //                    this.h1Title.InnerText = "修改付款记录";
    //                    PayoutRecord pr = PayoutRecord.GetPayoutRecord(int.Parse(Request.QueryString["PayoutRecordId"]));

    //                    gvPayoutListByOrderId.DataSource = pr.Order.PayoutRecords;
    //                    gvPayoutListByOrderId.DataBind();
    //                    this.imgBillAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", pr.Order.Bill.FinanceAudit.ToString());
    //                    this.imgMediaAudit.ImageUrl = string.Format("~/pics/audit{0}.gif", pr.Order.MediaFinanceAudit.ToString());
    //                    this.lblCustomerName.Text = pr.Order.Bill.Customer.Name;
    //                    this.lblOrderId.Text = pr.OrderId.ToString();
    //                    this.lblMediaName.Text = pr.Order.MediaInfo.Name;
    //                    this.lblBrokerageBalance.Text = (pr.DeductFromBrokerage + pr.Order.MediaInfo.Brokerage).ToString("");
    //                    this.lblAdvanceBalance.Text = (pr.DeductFromAdvance + pr.Order.MediaInfo.Advance).ToString("");
    //                    this.txtDeductFromAdvance.Text = pr.DeductFromAdvance.ToString();
    //                    this.txtDeductFromBrokerage.Text = pr.DeductFromBrokerage.ToString();
    //                    this.txtDescription.Text = pr.Description;
    //                    this.txtPayout.Text = pr.Payout.ToString();
    //                    this.txtTotalPayout.Text = (pr.Payout + pr.DeductFromAdvance + pr.DeductFromBrokerage).ToString("c");
    //                    this.lblMediaName.ToolTip = pr.PayoutUnitId.ToString();
    //                    this.txtPayoutDate.Text = pr.PayoutDate.ToString("d");
    //                    this.valcAdvanceBalace.ValueToCompare = (pr.DeductFromAdvance + pr.Order.MediaInfo.Advance).ToString();
    //                    this.valcBrokerage.ValueToCompare = (pr.DeductFromBrokerage + pr.Order.MediaInfo.Brokerage).ToString();
    //                    if ((pr.DeductFromAdvance + pr.Order.MediaInfo.Advance) <= 0)
    //                    {
    //                        txtDeductFromAdvance.Text = "没余额可扣";
    //                        txtDeductFromAdvance.Enabled = false;
    //                        this.valcAdvanceBalace.Enabled = false;
    //                    }
    //                    if ((pr.DeductFromBrokerage + pr.Order.MediaInfo.Brokerage) <= 0)
    //                    {
    //                        txtDeductFromBrokerage.Text = "没余额可扣";
    //                        txtDeductFromBrokerage.Enabled = false;
    //                        this.valcBrokerage.Enabled = false;
    //                    }
    //                    decimal FinalPayout = 0;
    //                    decimal AlreadyPayout = 0;
    //                    if(pr.Order.MediaOrders !=null)
    //                    {
    //                        foreach (MediaOrder mo in pr.Order.MediaOrders)
    //                        {
    //                            FinalPayout += mo.RealPayout;
    //                        }
    //                    }
    //                    if (FinalPayout != 0)
    //                    {
    //                        lblFinalPayout.Text = FinalPayout.ToString("c");
    //                    }
    //                    else { lblFinalPayout.Text = "没记录"; }
    //                    if(pr.Order.PayoutRecords != null)
    //                    {
    //                        foreach (PayoutRecord pr2 in pr.Order.PayoutRecords)
    //                        {
    //                            AlreadyPayout += pr2.Payout + pr2.DeductFromAdvance + pr2.DeductFromBrokerage;
    //                        }
    //                    }
    //                    if (AlreadyPayout != 0)
    //                    {
    //                        this.lbAlreadyPayout.Text = AlreadyPayout.ToString("c");
    //                    }
    //                    else { this.lbAlreadyPayout.Text = "没付款"; }

    //                }
    //            }
    //        }
    //    }

    //    if (CurrentOrder.Bill.FinanceAudit == Audit.Success)
    //    {
    //        this.PayoutRecordEditPlaceHolder.Visible = false;
    //    }
    //}

    //protected void btnSavePayout_Click(object sender, EventArgs e)
    //{
    //    if (Page.IsValid)
    //    {
    //        if (!Check())
    //            Utils.SetOperationResult(btnSavePayout, 
    //                "所填写的预存付款或反点数超出拥有的范围.", Request.RawUrl);
    //        PayoutRecord por = new PayoutRecord();
    //        string description = "用来付款";
    //        if (Request.QueryString["PayoutRecordId"] != null)
    //        {
    //            por = PayoutRecord.GetPayoutRecord(Convert.ToInt32(Request.QueryString["PayoutRecordId"]));
    //            description = "修改付款记录";
    //        }
    //        por.OrderId = Convert.ToInt32(this.lblOrderId.Text);
    //        if (this.txtPayout.Text != "")
    //        {
    //            por.Payout = Convert.ToDecimal(this.txtPayout.Text);
    //        }
    //        por.PayoutDate = Convert.ToDateTime(this.txtPayoutDate.Text);
    //        por.PayoutUnitId = Convert.ToInt32(this.lblMediaName.ToolTip);
    //        por.Description = this.txtDescription.Text;
    //        if (this.txtDeductFromAdvance.Text != "" && this.txtDeductFromAdvance.Text != "没余额可扣")
    //        {
    //            por.DeductFromAdvance = Convert.ToDecimal(this.txtDeductFromAdvance.Text);
    //        }
    //        if (this.txtDeductFromBrokerage.Text != "" && this.txtDeductFromBrokerage.Text !="没余额可扣")
    //        {
    //            por.DeductFromBrokerage = Convert.ToDecimal(this.txtDeductFromBrokerage.Text);
    //        }

    //        CurrentOrder.SavePayoutRecord(por);
    //        Response.Redirect("PayoutRecord.aspx?OrderId=" + por.OrderId, true);
    //    }
    //}

    //protected void gvPayoutListByOrderId_RowDataBound(object sender, GridViewRowEventArgs e)
    //{
    //    if(e.Row.RowType==DataControlRowType.DataRow)
    //    { 
    //        Label lblPayout = (Label)e.Row.FindControl("lblPayout");
    //        PayoutRecord pr = e.Row.DataItem as PayoutRecord;
    //        decimal payOut = pr.Payout + pr.DeductFromAdvance + pr.DeductFromBrokerage;
    //        lblPayout.Text = payOut.ToString("c");
    //    }
    //}

    //protected void gvPayoutListByOrderId_RowDeleting(object sender, GridViewDeleteEventArgs e)
    //{
    //    int recordId = Convert.ToInt32(this.gvPayoutListByOrderId.DataKeys[e.RowIndex].Values[0].ToString());
    //    CurrentOrder.RemovePayoutRecord(recordId, Resources.Labels.RemovePayoutRecord);
    //    Response.Redirect("PayoutRecord.aspx?OrderId=" + CurrentOrder.Id);
    //}

    //protected void btnNew_Click(object sender, EventArgs e)
    //{
    //    PayoutRecord pr = PayoutRecord.GetPayoutRecord(Convert.ToInt32(Request.QueryString["PayoutRecordId"]));
    //    Response.Redirect("PayoutRecord.aspx?OrderId=" + pr.OrderId);
    //}

    //protected void btnBack_Click(object sender, EventArgs e)
    //{
    //    if (Request.QueryString["PayoutRecordId"] != null)
    //    {
    //        PayoutRecord pr = PayoutRecord.GetPayoutRecord(int.Parse(Request.QueryString["PayoutRecordId"]));
    //        Response.Redirect("~/Finance/Accounts.aspx?BillId=" + pr.Order.BillId);
    //    }
    //    else if (Request.QueryString["OrderId"] != null)
    //    {
    //        Order odr = Order.GetOrder(int.Parse(Request.QueryString["OrderId"]));
    //        Response.Redirect("~/Finance/Accounts.aspx?BillId=" + odr.BillId);
    //    }
    //}

    //#region method
    //protected bool Check()
    //{
    //    decimal advance = 0;
    //    decimal brokerage = 0;
    //    if (this.txtDeductFromAdvance.Text != "" && this.txtDeductFromAdvance.Text != "没余额可扣")
    //    {
    //        advance = Convert.ToDecimal(this.txtDeductFromAdvance.Text);
    //    }
    //    if (this.txtDeductFromBrokerage.Text != "" && this.txtDeductFromBrokerage.Text != "没余额可扣")
    //    {
    //        brokerage = Convert.ToDecimal(this.txtDeductFromBrokerage.Text);
    //    }
    //    if (CurrentMediaInfo.Advance <= advance || CurrentMediaInfo.Brokerage <= brokerage)
    //        return false;
    //    return true;
    //}

    //protected void SaveBrokerage()
    //{
 
    //}

    //protected void SaveAdvance()
    //{
 
    //}
    //#endregion
}
